from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
import requests
from login.setting import *
from .verify import *
# Create your views here.


@verify_control
def add_build(request):
    """
            @author:薛威宁
            @date:2018/10/16/10:40
            为add_tower_api提供数据
            提交方式 ：requests.post
            提交数据格式 ：{"tower_name'":'xxx'}
            @ 整理：董海明
        """
    info = dict(request.POST)
    if info["tower_name"][0] == "":
        context = {}
        return render(request, "tower/add_building.html", context)
    tower_url = url_ip + "apitower/add_tower_api/"
    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0]})
    print(r.text)
    if "楼房添加成功" == r.json()["message"]:
        context = {}
        return render(request, "tower/成功.html", context)
    else:
        return HttpResponse(r.json()["message"])


@verify_control
def add_floor(request):
    """
            @author:薛威宁
            @date:2018/10/16/10:40
            为add_floor_api接口提供所需要数据
            提交方式 ：requests.post
            提交数据格式 ：{"tower_name":"xxx", "floor_name":"xxx"}
            @ 整理：董海明
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "" or info["floor_name"][0] == "":
        context = {}
        return render(request, "tower/floor.html", context)
    tower_url = url_ip + "apitower/add_floor_api/"
    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0], "floor_name": info["floor_name"][0]})
    if "楼层添加成功" == r.json()["message"]:
        context = {}
        return render(request, "tower/成功.html", context)
    else:
        return HttpResponse(r.json()["message"])


@verify_control
def add_room(request):
    """
        @author:薛威宁
        @date:2018/10/16/10:40
        为add_room_api接口提供数据
        提交方式 ：requests.post
        提交数据格式 ：{"tower_name":"xxx", "floor_name": "xxx" , "room_name": "xxx"}
        @ 整理： 董海明
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "" or info["floor_name"][0] == "" or info["room_name"][0]:
        context = {}
        return render(request, "tower/room.html", context)
    tower_url = url_ip + "apitower/add_room_api/"

    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0],
                                       "floor_name": info["floor_name"][0],
                                       "room_name": info["room_name"][0]})
    if "房间添加成功" == r.json()["message"]:
        context = {}
        return render(request, "tower/成功.html", context)
    else:

        return HttpResponse(r.json()["message"])


@verify_control
def del_tower(request):
    """
    删除楼房
        @author:郭峰
        @date:2018/10/16
        为del_towre_api接口提供数据，
        提交方式 ：requests.post
        提交数据格式 ：{"tower_name":"xxx"}
        @ 整理：董海明
            删除楼房会联级删除 楼层、房间、资产，请谨慎操作
    """

    info = dict(request.POST)
    if info["tower_name"][0] == "":
        context = {}
        return render(request, "tower/del_api.html", context)
    tower_url = url_ip + 'tower/del_tower/'
    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0]})
    if "删除成功" ==r.json()["message"]:
        context = {"message": r.json()["message"]}
        return render(request, "tower/del_api.html", context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "tower/del_api.html", context)


@verify_control
def api_floor(request):
    """
        删除楼层
        @author:郭峰
        @date:2018/10/16
        为del_floor_api提供数据， 删除楼层会联级删除房间以及房间内的资产，请谨慎操作
        提交方式 ：requests.post
        提交数据格式 ：{"tower_name": "xxx", "floor_name":"xxx"}
        @ 整理：董海明
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "" or info["floor_name"][0] == "":
        context = {}
        return render(request, "tower/def_floor.html", context)
    tower_url = url_ip + "tower/del_floor/"
    r = requests.post(tower_url, data={"towre_name": info["tower_name"][0], "floor_name": info["floor_name"][0]})
    if "删除楼层成功" == r.json()["message"]:
        context = {"message": r.json()["message"]}
        return render(request, "tower/def_floor.html", context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "tower/def_floor.html", context)


@verify_control
def del_room(request):
    """
        删除房间
         @author:郭峰
        @date:2018/10/16
        为del_room_api提供数据， 删除房间会联级删除房间内的资产，请谨慎操作
        提交方式 ：requests.post
        提交数据格式 ：{"tower_name":"xxx",  "floor_name": "xxx", "room_name": "xxx"}
        @ 整理：董海明
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "" or info["floor_name"][0] == "" or info["room_name"][0]:
        context = {}
        return render(request, "tower/del_room.html", context)
    tower_url = url_ip + "apitower/del_room_api/"
    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0],
                                       "floor_name": info["floor_name"][0],
                                       "room_name": info["room_name"][0]})
    if "删除房间成功" == r.json()["message"]:
        context = {'message': r.json()['message']}
        return render(request, "tower/del_room.html",context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "tower/del_room.html", context)


@verify_control
def alter_tower(request):
    """
    为楼改名
    @author: 郭峰
    @date: 2018/10/16
    为change_tower_api提供数据，
    提交方式: requests.post
    提交数据格式: {"tower_name": "xxx", "new_tower_name": "xxx"}
    @整理： 董海明
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "":
        context = {}
        return render(request, "tower/change_tower.html", context)
    tower_url = url_ip + "apitower/change_tower_api/"
    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0],
                                       "new_tower_name": info["new_tower_name"][0]})
    if "楼名修改成功" == r.json()["message"]:
        context = {"message": r.json()["message"]}
        return render(request, "tower/change_tower.html",context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "tower/change_tower.html",context)


@verify_control
def alter_floor(request):
    """
        更改楼层名
        @author: 郭峰
        为change_floor_api提供数据
        @date: 2018/10/16
        提交方式: requests.post
        提交数据格式: {"tower_name":"xxx", "floor_name":"xxx", "new_floor_name":"xxx"}
        @ 整理：董海明
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "" or info["floor_name"][0] == "" or info["new_floor_name"][0] == "":
        context = {}
        return render(request, "tower/change_floor.html", context)
    tower_url = url_ip + "apitower/change_floor_api/"
    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0],
                                       "floor_name": info["floor_name"][0],
                                       "new_floor_name": info["new_floor_name"][0]})
    if "楼层信息修改成功" == r.json()["message"]:
        context = {"message": r.json()["message"]}
        return render(request, "tower/change_floor.html", context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "tower/change_floor.html", context)


@verify_control
def alter_room(request):
    """
        更改房间名称
        @author: 郭峰
        @date: 2018/10/16
        为change_room_api接口提供数据
        提交方式: requests.post
        提交数据格式: { "tower_name":"xxx", "floor_name":"xxx", "room_name":"xxx", "new_room_name":"xxx"}
        @整理： 董海明
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "" or info["floor_name"][0] == "" or \
            info["room_name"][0] == "" or info["new_room_name"][0] == "":
        context = {}
        return render(request, "tower/change_room.html", context)
    tower_url = url_ip + "apitower/change_room_api/"
    r = requests.post(tower_url, data={"tower_name": info["tower_name"][0],
                                       "floor_name": info["floor_name"][0],
                                       "room_name": info["room_name"][0],
                                       "new_room_name": info["new_room_name"][0]})

    if "房间信息修改成功" == r.json()["message"]:
        context = {"message": r.json()["message"]}
        return render(request, "tower/change_room.html", context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "tower/change_room.html", context)











